package com.gome.spiderkit.client;

import java.util.ArrayList;
import java.util.concurrent.TimeoutException;

import com.google.protobuf.InvalidProtocolBufferException;

public class WebElement {
	
	private WebKitMQ m_webkit_mq;
	private long m_ref;
	private long m_page_ref;
	private String m_ref_str;
	
	public WebElement()
	{
		this.m_webkit_mq = null;
		this.m_ref = 0;
		this.m_page_ref = 0;
		this.m_ref_str = null;
	}
	
	public WebElement(long element_ref, long page_ref, WebKitMQ webkit_mq) 
	{
		this.m_ref = element_ref;
		this.m_page_ref = page_ref;
		this.m_webkit_mq = webkit_mq;
		this.m_ref_str = String.valueOf(m_ref);
	}
	
	public boolean isFailed()
	{
		return m_webkit_mq.failed();
	}

	public String getTagName() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETTAGNAME_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getAttribute(String attribute) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(attribute);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETATTRIBUTE_LONG_REF_STRING_NAME, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getProperty(String property) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(property);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETPROPERTY_LONG_REF_STRING_NAME, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getText() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETTEXT_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getValue() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETVALUE_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
		
	public boolean type(String val) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(val);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_TYPE_LONG_REF_STRING_VAL, argList);
		return m_webkit_mq.getBoolean(msg);
	}
	
	public boolean setValue(String val) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(val);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SETVALUE_LONG_REF_STRING_VAL, argList);
		return m_webkit_mq.getBoolean(msg);
	}
	
	public boolean submit() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SUBMIT_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);
	}
	
	
	public boolean reset() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_RESET_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}

	public boolean toggle() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_TOGGLE_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}
	
	public boolean focus() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_FOCUS_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}
	
	public boolean selected() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SELECTED_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}
	
	public boolean selected(boolean selected) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(selected));
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SETSELECTED_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}

	public boolean checked() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_CHECKED_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}
	
	public boolean checked(boolean checked) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(checked));
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SETCHECKED_LONG_REF, argList);
		return m_webkit_mq.getBoolean(msg);		
	}
	
	public long optionIndex() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_OPTIONINDEX_LONG_REF, argList);
		return m_webkit_mq.getLong(msg);		
	}
	
	public long stale() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_STALE_LONG_REF, argList);
		return m_webkit_mq.getLong(msg);		
	}
	
	public boolean isVisible() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_BOOL_ISVISIBLE_LONG_REF, argList);
		return m_webkit_mq.getLong(msg) == 0 ? false : true;		
	}
	
	public long drag(int toX,int toY,int duration) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(String.valueOf(toX));
		argList.add(String.valueOf(toY));
		argList.add(String.valueOf(duration));
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_DRAG_LONG_REF_INT_TOX_INT_TOY_INT_DURATION, argList);
		return m_webkit_mq.getLong(msg);		
	}
	
	public Rect getRect() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_RECT_GETRECT_LONG_REF, argList);
		return m_webkit_mq.getRect(msg);		
	}
	
	public String getInnerXml() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETINNERXML_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public String getOuterXml() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_STRING_GETOUTERXML_LONG_REF, argList);
		return m_webkit_mq.getString(msg);
	}
	
	public boolean click() throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add("0");
		String msg = m_webkit_mq.webKitCall(getPageRef(), WebKitProtocol.WEBELEMENT_LONG_CLICK_LONG_REF, argList);
		WebPageRef webpageRef = new WebPageRef();
		long ret = m_webkit_mq.getPageValue(msg, webpageRef);
		if(ret < 0)
			return false;
		
		return true;
	}
	
	public boolean sendKeys(String type,long keys) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(type);
		argList.add(String.valueOf(keys));
		argList.add("0");
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_LONG_SENDKEYS_LONG_REF_STRING_TYPE_LONG_KEYS, argList);
		WebPageRef webpageRef = new WebPageRef();
		long ret = m_webkit_mq.getPageValue(msg, webpageRef);
		if(ret < 0)
			return false;
		
		return true;

	}
	
	public void setAttribute(String name, String value) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(name);
		argList.add(value);
		m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_VOID_SETATTRIBUTE, argList);	
	}
	
	public void removeAttribute(String name) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(name);
		m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_VOID_REMOVEATTRIBUTE, argList);	
	}
	
	public ArrayList<WebElement> getChildsByCssSelector(String query) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException {
		ArrayList<WebElement> elements = new ArrayList<WebElement>();
		ArrayList<String> argList = new ArrayList<String>();
		argList.add(m_ref_str);
		argList.add(query);
		String msg = m_webkit_mq.webKitCall(m_page_ref, WebKitProtocol.WEBELEMENT_VOID_GETCHILDSBYCSSSELECTOR, argList);
		ArrayList<Long> list = m_webkit_mq.getLongArrayList(msg);
		if (list != null) {
			for(Long element_ref:list)
			{
				WebElement element = new WebElement(element_ref,m_page_ref,m_webkit_mq);
				elements.add(element);
			}
		}
		return elements;
	}
	
	public boolean waitForInnerXml(int timeout) throws UnknowServerException, InvalidProtocolBufferException, TimeoutException
	{
		if (m_webkit_mq.failed())
			return false;
		
		int tryTime = 200;
		int tryCount = timeout/tryTime;
		int tryMod = timeout%tryTime;
		if(tryMod != 0)
			++tryCount;
		int count = 0;
		while(++count <= tryCount)
		{
			try {
				Thread.sleep(tryTime);
				String xml = this.getInnerXml();
				if(xml == "")
					continue;
				else
					return true;
						
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public long getRef() {
		return m_ref;
	}

	public long getPageRef() {
		return m_page_ref;
	}
}
